package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqut.dto.ScoreManagerDTO;
import com.cqut.dto.LimitShowDTO;
import com.cqut.model.ScoreManager;
import com.cqut.service.IScoreManagerService;
import com.cqut.util.ResponseUtil;


@RestController
@RequestMapping("/api/scoreManager")
public class ScoreManagerController {
    @Resource(name="scoreManagerService")
    private IScoreManagerService scoreManagerService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> scoreManagerList = scoreManagerService.loadMaps(null, null, null, null, limitShowDTO.getCurPage(), limitShowDTO.getLimit());
        long count = scoreManagerService.count();

        result.put("rows", scoreManagerList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    @RequestMapping(value = "/{scoreId}", method = RequestMethod.GET)
    public ResponseEntity<String> show(@PathVariable("scoreId") String scoreId) {
        Map<String, Object> scoreManagerModel = scoreManagerService.findMap(scoreId);

        return ResponseUtil.getResEntityForGetAndJson(scoreManagerModel);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        ScoreManagerDTO scoreManagerDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        ScoreManager scoreManagerModel = scoreManagerDTO.toModel();

        return ResponseUtil.getResEntityForPPP(scoreManagerService.save(scoreManagerModel));
    }

    @RequestMapping(value = "/{scoreId}", method = RequestMethod.PUT)
    public ResponseEntity<String> update(@PathVariable("scoreId") String scoreId,
        @Validated
        ScoreManagerDTO scoreManagerDTO, BindingResult errors) {

        ScoreManager scoreManagerModel = scoreManagerDTO.toModel();
        scoreManagerModel.setScoreId(scoreId);
        return ResponseUtil.getResEntityForPPP(scoreManagerService.update(scoreManagerModel));
    }

    @RequestMapping(value = "/{scoreId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory(@PathVariable("scoreId") String scoreId) {
        return ResponseUtil.getResEntityForDel(scoreManagerService.delete(scoreId));
    }

    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public ResponseEntity<String> deletes(@RequestParam("primaryKeys") String[] primaryKeys) {
        return ResponseUtil.getResEntityForDel(scoreManagerService.batchDelete(primaryKeys));
    }
}
